package org.xp.judgeplatform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.xp.judgeplatform.pojo.entity.ProblemSet;

@Mapper
public interface ProblemSetMapper extends BaseMapper<ProblemSet> {
    /**
     * 检查邀请码是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM problem_sets WHERE invite_code = #{inviteCode}")
    boolean existsByInviteCode(@Param("inviteCode") String inviteCode);

    /**
     * 更新成员数量
     */
    @Update("UPDATE problem_sets SET member_count = " +
            "(SELECT COUNT(*) FROM problem_set_members WHERE set_id = #{setId}) " +
            "WHERE id = #{setId}")
    void updateMemberCount(@Param("setId") Long setId);

    /**
     * 更新题目数量
     */
    @Update("UPDATE problem_sets SET problem_count = " +
            "(SELECT COUNT(*) FROM problem_set_items WHERE set_id = #{setId}) " +
            "WHERE id = #{setId}")
    void updateProblemCount(@Param("setId") Long setId);
}
